package com.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.pojo.Cart;
import com.pojo.Product;
import com.vo.CartVo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author lifeng
 * @create 2022-11-28 10:59
 */
@Repository
public interface CartDao extends BaseMapper<Cart> {

    @Insert("insert into cart values(null,#{productId},#{user.username},null,null,#{count})")
    void insertByCartVo(CartVo cartVo);

    /**
     * 查询所有订单根据用户名
     * @return
     */
    @Select("SELECT * FROM cart c,t_product t WHERE c.`p_id` = t.`id` AND c.username = #{username} AND c.status = 0")
    @Results(value = {
            @Result(property = "id",column = "id"),
            @Result(property = "pId",column = "p_id"),
            @Result(property = "username",column = "username"),
            @Result(property = "createTime",column = "create_time"),
            @Result(property = "updateTime",column = "update_time"),
            @Result(property = "count",column = "count"),
            @Result(
                    property = "product",
                    column = "p_id",
                    javaType = Product.class,
                    one = @One(
                    select = "com.dao.ProductDao.selectById"
            ))
    })
    List<Cart> selectAllByOne(String username);
}
